// const { IFRAME_ORGIN } = process.globalVars
// const env = getEnv()
//
// const ENV_TYPE = env === 'uat' ? 'pre' : env;
// // 生成 iframe 配置
// const createIFrameOrgin = () => {
//   if (!IFRAME_ORGIN) return {};
//   const keys = Object.keys(IFRAME_ORGIN);
//   if(!(Array.isArray(keys) && keys.length)) return {};
//   return keys.reduce((result, key) => {
//     result[key] = (IFRAME_ORGIN[key] || {})[ENV_TYPE];
//   }, {})
// }
//
// export const iframeOrigin = createIFrameOrgin()

/** setCookie (设置 Cookie)方法说明
* @author: JasonStandFor
* @description Desc:: 使用 encodeURI 编码格式存入
* @param {string} key - 存放的唯一标识
* @param {string} value - 使用 JSON.stringify(obj) 存放的内容
* @param {number} days - 默认 7 天时限，超出自动清除
* @return undefined
* @example:
setCookie('userInfo', JSON.stringify(userInfo), 1);
* */
export const setCookie = (key, value, days = 7) => {
  const exp = new Date();
  let newValue = null;
  exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
  if (typeof value === 'string') {
    newValue = value;
  } else {
    newValue = JSON.stringify(value);
  }
  // 强行加入 =%2B%3X，防止三方工具解码
  window.document.cookie = `${key}=%2B%3X${encodeURI(newValue)};expires=${exp.toGMTString()}`;
};

/** getCookie (获取 Cookie)方法说明
* @author: JasonStandFor
* @description Desc:: 使用 decodeURI 编码格式取出
* @param {string} key - 存放的唯一标识
* @return cookieValue
* @example:
const userInfo = getCookie('userInfo');
console.log('userInfo: ', userInfo);
* */
export const getCookie = (key) => {
  let catchCookie;
  const reg = new RegExp(`(^| )${key}=([^;]*)(;|$)`);
  const curCookieList = window.document.cookie.match(reg) ?? [];
  try {
    if (curCookieList[2]) {
      // 清除 =%2B%3X 后解码
      catchCookie = JSON.parse(decodeURI(curCookieList[2].substr(6)));
    } else {
      catchCookie = null;
    }
  } catch (err) {
    console.error(err);
  }
  return catchCookie;
};

/** delCookie (删除 Cookie)方法说明
* @author: JasonStandFor
* @description Desc:: 调用 delCookie 方法进行删除操作
* @param {string} key - 存放的唯一标识
* @return undefined
* @example:
delCookie('userInfo');
* */
export const delCookie = (key) => {
  var exp = new Date();
  exp.setTime(exp.getTime() - 1);
  const cval = getCookie(key);
  if (cval !== null) {
    window.document.cookie = `${key}=${cval};expires=${exp.toGMTString()}`;
  }
};
